package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.FreightTemplateChargingRegionDAO;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplate;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateCharging;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateChargingRegion;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateChargingRegion.Column;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateChargingRegionExample;
import com.ecoolex.framework.common.util.Check;


@Service
public class FreightTemplateChargingRegionManager {

	@Autowired
	private FreightTemplateChargingRegionDAO dao;

	public void update(FreightTemplateCharging charging, List<Integer> regionList, String updateUser) {
		// 删除所有已经存在
		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andChargingIdEqualTo(charging.getChargingId());
		dao.deleteByExample(example);

		List<FreightTemplateChargingRegion> result = regionList.stream().map(regionId -> {
			FreightTemplateChargingRegion record = new FreightTemplateChargingRegion();

			record.setFreightTemplateId(charging.getFreightTemplateId());
			record.setChargingId(charging.getChargingId());
			record.setRegionId(regionId);
			record.setDeleted(FreightTemplateChargingRegion.NOT_DELETED);
			record.setCreateUser(updateUser);
			record.setCreateTime(Date.from(Instant.now()));
			record.setUpdateUser(updateUser);
			record.setUpdateTime(Date.from(Instant.now()));
			return record;
		}).collect(Collectors.toList());
		// 批量添加
		dao.batchInsert(result);
	}

	public void deleteByFreightTemplate(FreightTemplate template, String updateUser) {
		FreightTemplateChargingRegion record = new FreightTemplateChargingRegion();
		record.setDeleted(FreightTemplateChargingRegion.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));

		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andFreightTemplateIdEqualTo(template.getFreightTemplateId()).andDeletedEqualTo(
			FreightTemplateCharging.NOT_DELETED);
		dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
	}

	public long countByChargingId(Integer chargingId) {
		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andChargingIdEqualTo(chargingId).andDeletedEqualTo(
			FreightTemplateCharging.NOT_DELETED);
		return dao.countByExample(example);
	}

	public List<FreightTemplateChargingRegion> queryByChargingId(Integer chargingId, Integer size) {
		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andChargingIdEqualTo(chargingId).andDeletedEqualTo(
			FreightTemplateCharging.NOT_DELETED);
		if (Check.notNullAndGtZero(size)) {
			example.setRows(size);
		}
		return dao.selectByExample(example);
	}

	public FreightTemplateChargingRegion getByFreightTemplateIdAndRegionId(Integer freightTemplateId, Integer regionId) {
		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andFreightTemplateIdEqualTo(freightTemplateId).andRegionIdEqualTo(
			regionId).andDeletedEqualTo(FreightTemplateCharging.NOT_DELETED);
		return dao.selectOneByExample(example);
	}
	
	public FreightTemplateChargingRegion getByFreightTemplateId(Integer freightTemplateId) {
		FreightTemplateChargingRegionExample example = new FreightTemplateChargingRegionExample();
		example.createCriteria().andFreightTemplateIdEqualTo(freightTemplateId).andDeletedEqualTo(FreightTemplateCharging.NOT_DELETED);
		return dao.selectOneByExample(example);
	}
}